﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using CommomFunctions;

namespace Pilhas
{
    public class Controls : CommomFunctions.Controls
    {
        /// <summary>
        /// blueArrow -> image used to point the element at the top of the stack
        /// txtLetters -> the textBox where the user inserts the stack element's label when s/he wants to customize it
        /// stackElements -> the ellipses that will represent the elements in the stack.
        /// elemLables -> each element in the stack has a label to differ it from other elements
        /// </summary>
        protected static Image blueArrow = new Image();
        protected static TextBox txtLetters = new TextBox();
        protected static Ellipses stackElements = new Ellipses();
        protected static Labels elemLabels = new Labels();
                
        /// <summary>
        /// Adds to "gridi" the controls that were not declared/created in the xaml file.
        /// </summary>
        /// <param name="gridi">
        /// The application Grid where the controls are added.
        /// </param>
        public void addOtherControls(Grid gridi)
        {
            gd = gridi;
            stackElements.createEllipses(gd);
            elemLabels.createLabels(gd, stackElements.numElements);                       
            
            createLogo(2,1,new Thickness(0, 230, 10, 0));
            createTitle("Manipulação de Pilhas", 2, 4, new Thickness(0, 5, 0, 0));
            createTrace(2, 3, 2, 1, new Thickness(105, 40, 15, 0), 200, 270);

            createTxtLetters();

            createArrow();
        }

        /// <summary>
        /// Creates the blue arrow that will point the element at the top of the stack
        /// </summary>
        private void createArrow()
        {
            blueArrow.Source = Imagens.loadBitmap(Pilhas.Properties.Resources.blueArrow);
            blueArrow.Stretch = Stretch.Fill;
            blueArrow.HorizontalAlignment = HorizontalAlignment.Left;
            blueArrow.VerticalAlignment = VerticalAlignment.Center;
            blueArrow.Visibility = Visibility.Hidden;
            
            gd.Children.Add(blueArrow);
            Grid.SetRow(blueArrow, 2);
            Grid.SetColumn(blueArrow, 0);
        }

        /// <summary>
        /// Creates the TextBox that will receive that customized labels for the stack elements.
        /// </summary>
        private void createTxtLetters()
        {
            txtLetters.Visibility = Visibility.Hidden;
            txtLetters.Margin = new Thickness(115, 115, 30, 20);
            txtLetters.Height = 25.0;
            txtLetters.MaxWidth = 90;
            txtLetters.HorizontalAlignment = HorizontalAlignment.Stretch;
            txtLetters.VerticalAlignment = VerticalAlignment.Top;

            gd.Children.Add(txtLetters);
            Grid.SetRow(txtLetters, 2);
            Grid.SetColumn(txtLetters, 4);
            Grid.SetColumnSpan(txtLetters, 2);
        }
        
        /// <summary>
        /// Updates the blueArrow size and position when the grid size changes
        /// </summary>
        public void gridSizeChanged()
        {
            blueArrow.Height = 51;
            blueArrow.Width = 70;
            ControlVariables.updateImageMargin();            
        }
         
        /// <summary>
        /// Calls the respective button function
        /// </summary>
        /// <param name="bc"></param>
        public void buttonClicked(ButtonClicked bc)
        {
            switch (bc)
            {
                case ButtonClicked.Pop: Buttons.btnPop_Click();
                    break;
                case ButtonClicked.Push: Buttons.btnPush_Click();
                    break;
            }
        }

        public void buttonClicked(ButtonClicked bc, Rectangle rectangle1)
        {            
            switch (bc)
            {
                case ButtonClicked.Create: Buttons.btnCreate_Click(btns, rectangle1);
                    break;
                case ButtonClicked.Destroy: Buttons.btnDestroy_Click(btns, rectangle1);
                    break;                
            }
        }

        /// <summary>
        /// If "useLetters" is selected then a new stack which elements hava customized labels will be created
        /// </summary>
        /// <param name="usarLetters"></param>
        /// <param name="ret"></param>
        public void useLettersClicked(MenuItem usarLetters, Rectangle ret)
        {
            if (Menus.usarLetras_Click(usarLetters, txtLetters))
                Buttons.btnCreate_Click(btns, ret);
        }
    }
}
